home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Text / Edit / GoldED-Demo / installdata / golded / developer / source / appicon / app.c next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  9.8 KB  |  515 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.  AppIcon handler, ©1998 Dietmar Eilert
  4.  
  5.  DICE:
  6.  
  7.  dcc app.c appiconA.a -// -proto -mRR -mi -pr -3.0 -o appicon
  8.  
  9.  SAS/C:
  10.  
  11.  sc app.c
  12.  
  13.  CONTENTS
  14.  
  15.  This is a simple AppIcon handler. It will show an application icon (we use the
  16.  editor's default appicon from etc/images/workbench/appicon) on the workbench
  17.  screen. Icons of text files may be dragged and dropped over this icon and the
  18.  editor will load them into a new window. Doubleclick at the icon to have it
  19.  removed.
  20.  
  21.  HOW TO SET THE DEFAULT ICON POSITION ...
  22.  
  23.  Open the etc/images/workbench drawer and move the icon to the preferred
  24.  position, snapshot it (workbench/icon menu). and move the icon back to its
  25.  drawer. The appicon handler will read the new position the next time it is
  26.  evoked.
  27.  
  28.   ------------------------------------------------------------------------------
  29. */
  30.  
  31. /// "compiler"
  32.  
  33. #ifdef __SASC
  34.  
  35. #define __USE_SYSBASE
  36. #define __geta4      __saveds
  37. #define __stkargs    __stdargs
  38.  
  39. #define __A0         register __a0
  40. #define __A1         register __a1
  41. #define __A2         register __a2
  42. #define __A3         register __a3
  43. #define __A4         register __a4
  44. #define __A5         register __a5
  45. #define __A6         register __a6
  46. #define __A7         register __a7
  47. #define __D0         register __d0
  48. #define __D1         register __d1
  49. #define __D2         register __d2
  50. #define __D3         register __d3
  51. #define __D4         register __d4
  52. #define __D5         register __d5
  53. #define __D6         register __d6
  54. #define __D7         register __d7
  55.  
  56. #endif
  57.  
  58. #ifdef _DCC
  59.  
  60. #define __asm
  61.  
  62. #endif
  63.  
  64. ///
  65. /// "includes"
  66.  
  67. #include <exec/exec.h>
  68. #include <string.h>
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <stdlib.h>
  72. #include <stdarg.h>
  73. #include <intuition/intuition.h>
  74. #include <dos/dos.h>
  75. #include <dos/dosextens.h>
  76. #include <dos/rdargs.h>
  77. #include <dos/dostags.h>
  78. #include <workbench/startup.h>
  79. #include <workbench/workbench.h>
  80. #include <rexx/errors.h>
  81. #include <rexx/rxslib.h>
  82.  
  83. // prototypes
  84.  
  85. #include <clib/alib_protos.h>
  86. #include <clib/dos_protos.h>
  87. #include <clib/exec_protos.h>
  88. #include <clib/icon_protos.h>
  89. #include <clib/intuition_protos.h>
  90. #include <clib/utility_protos.h>
  91. #include <clib/rexxsyslib_protos.h>
  92. #include <clib/wb_protos.h>
  93.  
  94. // pragmas
  95.  
  96. #ifdef __SASC
  97.  
  98. #include <pragmas/rexxsyslib_pragmas.h>
  99. #include <pragmas/utility_pragmas.h>
  100. #include <pragmas/wb_pragmas.h>
  101. #include <pragmas/dos_pragmas.h>
  102. #include <pragmas/icon_pragmas.h>
  103. #include <pragmas/intuition_pragmas.h>
  104. #include <pragmas/exec_sysbase_pragmas.h>
  105.  
  106. #endif
  107.  
  108. ///
  109. /// "globals"
  110.  
  111. #ifdef __SASC
  112.  
  113. UBYTE Version[] = "$VER: appicon 3.2 " __AMIGADATE__ "\n\0";
  114.  
  115. #endif
  116.  
  117. #ifdef _DCC
  118.  
  119. UBYTE Version[] = "$VER: appicon 3.2 (" __COMMODORE_DATE__ ")\n\0";
  120.  
  121. #endif
  122.  
  123. extern struct Library *IconBase;
  124. extern struct Library *DOSBase;
  125. extern struct Library *SysBase;
  126. extern struct Library *IntuitionBase;
  127. extern struct Library *WorkbenchBase;
  128. extern struct Library *RexxSysBase;
  129.  
  130. ///
  131. /// "defines"
  132.  
  133. #define Prototype    extern
  134. #define MAX_PATHLEN  512
  135.  
  136. ///
  137. /// "prototypes"
  138.  
  139. Prototype int    main(ULONG, char **);
  140. Prototype int    wbmain(struct WBStartup *);
  141. Prototype int    MainLoop(void);
  142. Prototype UBYTE *MakeFileName (UBYTE *, UBYTE *);
  143. Prototype UBYTE *CompletePath(UBYTE *);
  144. Prototype UBYTE *StartGED(void);
  145. Prototype struct RexxMsg *SendRexxCommand(UBYTE *, UBYTE *, struct MsgPort *);
  146. Prototype void   FreeRexxCommand (struct RexxMsg *);
  147. Prototype ULONG  WaitForAnswer(struct MsgPort *);
  148. Prototype UBYTE *LookForGED(void);
  149. Prototype void   ReadWBCmd(ULONG, struct WBArg *);
  150.  
  151. ///
  152. /// "entry points"
  153.  
  154. int
  155. main(argc, argv)
  156.  
  157. ULONG argc;
  158. char *argv[];
  159. {
  160.     return(MainLoop());
  161. }
  162.  
  163. int
  164. wbmain(struct WBStartup *wbs)
  165. {
  166.     return(MainLoop());
  167. }
  168.  
  169.  
  170. ///
  171. /// "main loop"
  172.  
  173. /* --------------------------------- MainLoop ----------------------------------
  174.  
  175.  Open AppIcon, handle incoming messages
  176.  
  177. */
  178.  
  179. int
  180. MainLoop()
  181. {
  182.     struct DiskObject *appDiskObject;
  183.     int                error;
  184.  
  185.     error = 0;
  186.  
  187.     appDiskObject = GetDiskObject("golded:etc/images/wb/appicon");
  188.  
  189.     if (appDiskObject == NULL)
  190.  
  191.         appDiskObject = GetDefDiskObject(WBTOOL);
  192.  
  193.     if (appDiskObject) {
  194.  
  195.         struct MsgPort    *msgPort;
  196.         struct AppMessage *amsg;
  197.         struct AppIcon    *appIcon;
  198.  
  199.         if (msgPort = CreateMsgPort()) {
  200.  
  201.             if (appIcon = AddAppIconA(0, NULL, "GoldED", msgPort, NULL, appDiskObject, TAG_END)) {
  202.  
  203.                 BOOL terminated = FALSE;
  204.  
  205.                 while (terminated == FALSE) {
  206.  
  207.                     while (amsg = (struct AppMessage *)GetMsg(msgPort)) {
  208.  
  209.                         if (amsg->am_NumArgs)
  210.                             ReadWBCmd(amsg->am_NumArgs, amsg->am_ArgList);
  211.                         else
  212.                             terminated = TRUE;
  213.  
  214.                         ReplyMsg((struct Message *)amsg);
  215.                     }
  216.  
  217.                     WaitPort(msgPort);
  218.                 }
  219.             }
  220.             else {
  221.  
  222.                 error = 20;
  223.  
  224.                 puts("Couldn't allocate AppIcon. Workbench closed ?!");
  225.             }
  226.  
  227.             RemoveAppIcon(appIcon);
  228.  
  229.             DeleteMsgPort(msgPort);
  230.         }
  231.         else {
  232.  
  233.             error = 20;
  234.  
  235.             puts("Couldn't create message port ?!");
  236.         }
  237.  
  238.         FreeDiskObject(appDiskObject);
  239.     }
  240.     else {
  241.  
  242.         error = 20;
  243.  
  244.         puts("Error reading icon");
  245.     }
  246.  
  247.     return(error);
  248. }
  249.  
  250.  
  251. ///
  252. /// "misc"
  253.  
  254. /* ------------------------------- MakeFileName --------------------------------
  255.  
  256.  Build fully qualified path from file/path names; return pointer to static copy.
  257.  
  258. */
  259.  
  260. UBYTE *
  261. MakeFileName(path, file)
  262.  
  263. UBYTE *path, *file;
  264. {
  265.     static UBYTE buffer[MAX_PATHLEN];
  266.  
  267.     strcpy(buffer, "\42");
  268.  
  269.     strcat(buffer, path);
  270.  
  271.     CompletePath(buffer);
  272.  
  273.     strcat(buffer, file);
  274.  
  275.     strcat(buffer, "\42");
  276.  
  277.     return(buffer);
  278. }
  279.  
  280. /* ------------------------------ CompletePath -----------------------------------
  281.  
  282.  Add '/' to path if missing so far
  283.  
  284. */
  285.  
  286. UBYTE *
  287. CompletePath(path)
  288.  
  289. UBYTE *path;
  290. {
  291.     UWORD len;
  292.  
  293.     if (len = strlen(path))
  294.  
  295.         if ((path[len - 1] != ':') && (path[len - 1] != '/'))
  296.  
  297.             strcat(path, "/");
  298.  
  299.     return(path);
  300. }
  301.  
  302. /* ---------------------------------- ReadWBCmd --------------------------------
  303.  
  304.  Parse AppIcon message
  305.  
  306. */
  307.  
  308. void
  309. ReadWBCmd(numArgs, argList)
  310.  
  311. ULONG  numArgs;
  312. struct WBArg  *argList;
  313. {
  314.     UBYTE *host;
  315.  
  316.     Forbid();
  317.  
  318.     host = LookForGED();
  319.  
  320.     Permit();
  321.  
  322.     if (host == NULL)
  323.  
  324.         host = StartGED();
  325.  
  326.     if (host) {
  327.  
  328.         struct MsgPort *replyPort;
  329.  
  330.         if (replyPort = CreateMsgPort()) {
  331.  
  332.             if (SendRexxCommand(host, "LOCK CURRENT RELEASE=4", replyPort)) {
  333.  
  334.                 if (WaitForAnswer(replyPort) == RC_OK) {
  335.  
  336.                     UBYTE path[MAX_PATHLEN];
  337.  
  338.                     UWORD  count;
  339.                     UBYTE *command;
  340.  
  341.                     for (count = 0; numArgs--; count++) {
  342.  
  343.                         NameFromLock(argList[count].wa_Lock, path, sizeof(path));
  344.  
  345.                         command = MakeFileName(path, argList[count].wa_Name);
  346.  
  347.                         strins(command, "OPEN SMART QUIET ");
  348.  
  349.                         if (SendRexxCommand(host, command, replyPort))
  350.  
  351.                             WaitForAnswer(replyPort);
  352.                     }
  353.                 }
  354.  
  355.                 if (SendRexxCommand(host, "UNLOCK", replyPort))
  356.  
  357.                     WaitForAnswer(replyPort);
  358.             }
  359.  
  360.             DeleteMsgPort(replyPort);
  361.         }
  362.     }
  363. }
  364.  
  365.  
  366. /* ----------------------------------- LookForGED ----------------------------
  367.  
  368.  Look for running editor task
  369.  
  370. */
  371.  
  372. UBYTE *
  373. LookForGED()
  374. {
  375.     static UBYTE host[] = "GOLDED.1";
  376.  
  377.     UWORD try;
  378.  
  379.     for (try = '1'; try <= '9'; try++) {
  380.  
  381.         host[7] = try;
  382.  
  383.         if (FindPort(host))
  384.  
  385.             return(host);
  386.     } 
  387.  
  388.     return(NULL);
  389. }
  390.  
  391.  
  392. /* ------------------------------------- StartGED -----------------------------
  393.  
  394.  Launch a new editor task. Return pointer to host name (or NULL).
  395.  
  396. */
  397.  
  398. UBYTE *
  399. StartGED()
  400. {
  401.     struct MsgPort *port = NULL;
  402.  
  403.     if (SystemTags("golded:golded", SYS_Asynch, TRUE, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE) == 0) {
  404.  
  405.         UWORD try;
  406.  
  407.         for (try = 50; try; (port == NULL) && try--, Delay(10)) {
  408.  
  409.             Forbid();
  410.  
  411.             port = FindPort("GOLDED.1");
  412.  
  413.             Permit();
  414.         }
  415.     }
  416.  
  417.     return((port) ? "GOLDED.1" : NULL);
  418. }
  419.  
  420. ///
  421. /// "ARexx"
  422.  
  423. /* -------------------------------------- WaitForAnswer -----------------------
  424.  
  425.   Wait for answer on previously sent message. Free message afterwards. Primary
  426.   return code is returned.
  427.  
  428. */
  429.  
  430. ULONG
  431. WaitForAnswer(port)
  432.  
  433. struct MsgPort *port;
  434. {
  435.     struct RexxMsg *rexxMsg;
  436.     ULONG  result;
  437.  
  438.     result = 0;
  439.  
  440.     do {
  441.         
  442.         WaitPort(port);
  443.  
  444.         if (rexxMsg = (struct RexxMsg *)GetMsg(port))
  445.  
  446.             result = rexxMsg->rm_Result1;
  447.  
  448.     } while (!rexxMsg);
  449.  
  450.     FreeRexxCommand(rexxMsg);
  451.  
  452.     return(result);
  453. }
  454.  
  455.  
  456. /* ------------------------------------- FreeRexxCommand ----------------------
  457.  
  458.  Free ARexx message
  459.  
  460. */
  461.  
  462. void
  463. FreeRexxCommand(rexxmessage)
  464.  
  465. struct RexxMsg *rexxmessage;
  466. {
  467.     if (rexxmessage->rm_Result1 == RC_OK) 
  468.  
  469.         if (rexxmessage->rm_Result2)
  470.  
  471.             DeleteArgstring((char *)rexxmessage->rm_Result2);
  472.  
  473.     DeleteArgstring((char *)ARG0(rexxmessage));
  474.  
  475.     DeleteRexxMsg(rexxmessage);
  476. }
  477.  
  478.  
  479. /* ---------------------------------- SendRexxCommand -------------------------
  480.  
  481.  Send ARexx message
  482.  
  483. */
  484.  
  485. struct RexxMsg *
  486. SendRexxCommand(port, cmd, replyPort)
  487.  
  488. struct MsgPort *replyPort;
  489. UBYTE          *cmd,   *port;
  490. {
  491.     struct MsgPort *rexxport;
  492.     struct RexxMsg *rexx_command_message = NULL;
  493.  
  494.     Forbid();
  495.  
  496.     if (rexxport = FindPort(port)) {
  497.  
  498.         if (rexx_command_message = CreateRexxMsg(replyPort, NULL, NULL)) {
  499.  
  500.             if (rexx_command_message->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  501.  
  502.                 rexx_command_message->rm_Action = RXCOMM | RXFF_RESULT;
  503.  
  504.                 PutMsg(rexxport, &rexx_command_message->rm_Node);
  505.             }
  506.         }
  507.     }
  508.  
  509.     Permit();
  510.  
  511.     return(rexx_command_message);
  512. }
  513.  
  514. ///
  515.